package com.zzp.jap.test.controller;

import com.zzp.jap.test.applications.UserDto;
import com.zzp.jap.test.domain.UserRepository;
import com.zzp.jap.test.domain.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;
    @RequestMapping("/addUser")
    public void addNewUser(@RequestParam String name,@RequestParam String email){

        User user = User.builder().email(email).name(name).createTime(new Date()).build();

        userRepository.save(user);
    }

    @GetMapping("/getUser")
    public Page<UserDto> getLastName(){
        return userRepository.findByLastName("zzp",PageRequest.of(0,10));
    }

}
